<template>
	<view style='padding:10px;'>
		<u--form labelWidth='80px'>
			<u-form-item label="名称" prop="name" borderBottom ref="item1">
				<u--input v-model="name" border="none"></u--input>
			</u-form-item>
			<u-form-item label="电话" prop="name" borderBottom ref="item1">
				<u--input v-model="phone" border="none"></u--input>
				<u-button open-type="getPhoneNumber" @getphonenumber="getPhoneNumber" text='快速获取' slot="right" type='success'></u-button>
			</u-form-item>
		</u--form>
		<view style='padding-top:50px'>
			<u-button shape='circle' type='primary' text='成为推手' @click='submit'></u-button>
		</view>
	</view>
</template>

<script>
	export default {
		data() {
			return {
				name: '',
				phone: '',
				squadId: 0,
				staff: ''
			}
		},
		onReachBottom() {

		},
		onShow() {
			
			
		},
		onLoad(options) {
			// 页面有传小队id，就用传的
			if (typeof(options.scene) != 'undefined') {
				this.squadId = options.scene
				console.log('有参数',options.scene)
			}
			// 看是否有user，无的，就需要注册
			let token = uni.getStorageSync('api_token')
			if(token == ''){
				this.wxLogin()
			}else{
				this.checkStaff()
			}
		},
		methods: {
			wxLogin(){
				let _this = this
				uni.login({
					provider: "weixin",
					success: function(res) {
						let data = {
							code: res.code,
							recommendedUser: 2
						}
						console.log('信息',data)
						_this.request.get("wechat/mini/login", data).then((res) => {
							if (res.data.token) {
								uni.setStorageSync('api_token', res.data.token)
								_this.checkStaff()
							}
						})
					},
				});
			},
			checkStaff(){
				// 如果已经有团队，就不需要
				this.request.get(`api/staffs`).then((res) => {
					console.log('staff',res.data)
					if (res.data && res.data.length > 0) {
						this.staff = res.data[0]
						if (res.data[0].squad.id > 1) {
							uni.showModal({
								title: '',
								content: '您已经是推手，无需重复申请！',
								showCancel: false,
								success: () => {
									uni.switchTab({
										url: '/pages/mine/index'
									})
								}
							})
						}
					}
					this.request.get(`api/user`,{
						'@expands': "['entity.profile']"
					}).then((res) => {
						if(res.data.profile.__metadata.phone){
							this.phone = res.data.profile.__metadata.phone
						}
						// 核心在于squad的确定，这里有优先级，如果页面参数带的，就用页面参数的，没有的，就用推荐人的
						if(this.squadId == 0){
							this.request.get(`public/staffs`,{
								'@filter': `entity.getUser().getId() == ${res.data.recommendedUser}`
							}).then((res) => {
								if(res.data && res.data.length > 0){
									this.squadId = res.data[0].squad.id
									console.log('跟推荐人小队')
								}
							})
						}
					})
				})
			},
			getPhoneNumber(e) {
				let _this = this
				if (typeof(e.detail.iv) == 'undefined') {
					uni.showToast({
						title: '获取失败，请重试',
						icon: 'none'
					})
					return
				} else {
					uni.showLoading()
					uni.login({
						provider: "weixin",
						success: function(res) {
							let data = {
								code: res.code
							}
							_this.request.get("wechat/mini/login", data).then((res) => {
								uni.setStorageSync('api_token', res.data.token);
								if (res.data.data.session_key) {
									let data = {
										sessionKey: res.data.data.session_key,
										iv: e.detail.iv,
										encryptedData: e.detail.encryptedData,
									}
									_this.request.post(`wechat/decrypt-data`, data).then((res) => {
										_this.phone = res.data.phoneNumber
										console.log('phone', res);
										//更新手机号码
										_this.request.put(`api/user-profile`, {
											phone: res.data.phoneNumber
										}).then((res) => {
											uni.hideLoading()
										})
									})
								} else {
									uni.hideLoading()
									uni.showToast({
										title: '获取手机号码失败！',
										icon: 'none'
									})
								}
							})
						},
					});
				}
			},
			submit(){
				if(this.name.trim() == '' || this.phone.trim() == ''){
					uni.showToast({
						title: '请输入名称以及手机',
						icon: 'none'
					})
					return
				}
				let staff = {
					name: this.name,
					phone: this.phone,
					squad: this.squadId > 0 ? this.squadId : 1
				}
				console.log('staff资料',staff)
				if(this.staff == ''){
					this.request.post('api/staffs',staff).then((res) => {
						uni.showModal({
							title: '',
							content: '您已成功成为推手',
							showCancel:false,
							success: () => {
								uni.switchTab({
									url: '/pages/mine/index'
								})
							}
						})
					})
				}else{
					this.request.put(`api/staffs/${this.staff.id}`,staff).then((res) => {
						uni.showModal({
							title: '',
							content: '您已成功成为推手',
							showCancel:false,
							success: () => {
								uni.switchTab({
									url: '/pages/mine/index'
								})
							}
						})
					})
				}
			}
		}
	}
</script>

<style>

</style>